<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;
use Qwadmin\Model\PhotosModel;

class PhotosController extends ComController
{
    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $where['deleted'] = PhotosModel::STATUS_ON;
        $order = 'id desc';
        if (!empty($_GET['name'])) {
            $where['name'] = ['like','%'.$_GET['name'].'%'];
        }

        if (!empty($_GET['category'])) {
            $where['category'] = $_GET['category'];
            $this->assign('categorySlected',$_GET['category']);
        }
        $photos = M('photos');
        $pagesize = 10;//每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $photos->where($where)->count();

        $list = $photos->order($order)->where($where)->limit($offset . ',' . $pagesize)->select();
        if(!empty($list)){
            foreach ($list as $k=> &$v) {
                $v['category'] = PhotosModel::CATEGORY[$v['category']];
                $v['platform']  = PhotosModel::PLATFORM[$v['platform']];
            }
        }
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

//        pp($list);exit();
        $this->assign('category',PhotosModel::CATEGORY);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function add() {

        $this->assign('category',PhotosModel::CATEGORY);
        $this->assign('platform',PhotosModel::PLATFORM);
        $this->display('form');
    }

    public function edit() {

        $uid = isset($_GET['id']) ? intval($_GET['id']) : false;
        if (empty($uid)) {
           $this->error('参数错误');
        }
        $data = M('photos')->where(['id'=> $uid])->find();
        $this->assign('member',$data);
        $this->assign('category',PhotosModel::CATEGORY);
        $this->assign('platform',PhotosModel::PLATFORM);
        $this->display('form');
    }

    public function update() {

        $data = [
            'id'               => $_POST['id'],
            'title'             => $_POST['title'],
            'desc'          => $_POST['desc'],
            'link'          => $_POST['link'],
            'category'           => $_POST['category'],
            'platform'           => $_POST['platform'],
            'pic_url'           => $_POST['pic_url'],
        ];

        $model = M('photos');
        if (empty($data['id'])) { //增加
            if ($model->add($data)) {
               $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else { //编辑
            if ($model->save($data)) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        }

    }

    public function del() {

        $uids = isset($_REQUEST['uids']) ? $_REQUEST['uids'] : false;
        if(is_array($uids)){
            $map['id'] = array('in', $uids);
        }else{
            $map['id'] = $uids;
        }
        if (M('photos')->where($map)->delete()) {
            addlog('删除图片ID：' . $uids);
            $this->success('图片删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

}